﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Text;

namespace SmartMathLibrary.Space
{
    /// <summary>
    /// Zhis class provides the client connection to a space object server.
    /// </summary>
    public class SpaceClientProvider
    {
        /// <summary>
        /// This field holds the publish port of the object space server.
        /// </summary>
        private readonly int port;

        /// <summary>
        /// This field holds the publish name of the object space server.
        /// </summary>
        private readonly String name;

        /// <summary>
        /// This field holds the publish IP adress of the object space server.
        /// </summary>
        private readonly String ipAdress;

        /// <summary>
        /// This field holds the http channel.
        /// </summary>
        private readonly TcpChannel channel;

        /// <summary>
        /// Initializes a new instance of the <see cref="SpaceClientProvider"/> class.
        /// </summary>
        /// <param name="ipAdress">The IP adress of the object space server.</param>
        /// <param name="name">The publish name of the object space container.</param>
        /// <param name="port">The publish port of the object space container.</param>
        public SpaceClientProvider(String ipAdress, String name, int port)
        {
            if (name == (string) null)
            {
                throw new ArgumentNullException("name");
            }

            this.port = port;
            this.name = name;
            this.ipAdress = ipAdress;
            this.channel = new TcpChannel( );
        }

        /// <summary>
        /// Gets the publish port of the object space server.
        /// </summary>
        /// <value>The publish port of the object space server.</value>
        public int Port
        {
            get { return this.port; }
        }

        /// <summary>
        /// Gets the IP adress of the object space server.
        /// </summary>
        /// <value>The IP adress of the object space server..</value>
        public string IpAdress
        {
            get { return this.ipAdress; }
        }

        /// <summary>
        /// Gets the name of the object space server.
        /// </summary>
        /// <value>The name of the object space server.</value>
        public string Name
        {
            get { return this.name; }
        }

        /// <summary>
        /// Connects the current application with the object space server.
        /// </summary>
        public void Connect()
        {
            RemotingConfiguration.RegisterWellKnownClientType(typeof (SpaceContainer),
                                                              "tcp://" + this.ipAdress + ":" + this.port + "/" +
                                                              this.name);
        }

        /// <summary>
        /// Disconnects the current application with the object space server.
        /// </summary>
        public void Disconnect()
        {
            ChannelServices.UnregisterChannel(this.channel);
        }
    }
}